package com.code.app.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.View;

import com.code.app.R;
import com.code.app.base.BaseActivity;
import com.code.app.db.DaoSupportFactory;
import com.code.app.db.IDaoSupport;
import com.code.app.entities.Person;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Cancellable;
import io.reactivex.functions.Consumer;

public class MainActivity extends BaseActivity {

    private final static String TAG = "MainActivity";

    //定义权限数组
    private String[] mPermission = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    public static int PERMISSION_REQ = 0x16;
    //申请权限过程中 未通过的权限将保存到这个集合
    private List<String> mRequestPermission = new ArrayList<String>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initFunction(Bundle savedInstanceState) {
        //版本适配
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            for (String one : mPermission) {
                if (PackageManager.PERMISSION_GRANTED != this.checkPermission(one, Process.myPid(), Process.myUid())) {
                    mRequestPermission.add(one);
                }
            }
            if (!mRequestPermission.isEmpty()) {
                //如果有权限未通过 就申请权限
                //申请后将进入这个方法 onRequestPermissionsResult(int , String[] , int[] )
                this.requestPermissions(mRequestPermission.toArray(new String[mRequestPermission.size()]), PERMISSION_REQ);
            } else {
                //进入这里面表示申请权限已经全部通过 可以加入自己的逻辑
            }
        }

        IDaoSupport<Person> daoSupport = DaoSupportFactory.getInstance().getDao(Person.class);
    }

    /**
     * @param view
     */
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.anim:
                startActivity(new Intent(this, AnimActivity.class));
                break;
            case R.id.customview:
                startActivity(new Intent(this, CustomViewActivity.class));
                break;
            case R.id.webview:
                startActivity(new Intent(this, WebViewActivity.class));
                break;
            case R.id.notification:
                startActivity(new Intent(this, DownloaderActivity.class));
                break;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
            return;
        }
        if (requestCode == PERMISSION_REQ) {
            for (int i = 0; i < grantResults.length; i++) {
                for (String one : mPermission) {
                    if (permissions[i].equals(one) && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                        mRequestPermission.remove(one);
                    }
                }
            }
            if (!mRequestPermission.isEmpty()) {
                //进入这里面表示申请权限已经全部通过 可以加入自己的逻辑
                finish();
            }
        }
    }
}
